﻿package com.calefk.util {
	
	import flash.display.MovieClip;
	import flash.filters.ColorMatrixFilter;
	import flash.net.LocalConnection;
	
	public class Generic {
		public static function verifyInterval(max:Number, number:Number, minimum:Number):Boolean
		{
			return (max >= number && number > minimum);
		}

		public static function fillCharLeft(cad:String , charFill:String , 
				lenCharacters:int):String {
			var offset:int = lenCharacters - cad.length;
			var newcad:String = cad + ""; 
			for (var k:int = 0; k < offset; k++) {
				newcad = charFill + newcad ;
			}
			return newcad;
		}
		
		public static function getFormatTime(miliseconds:int):Object {
			var secondsTotal:int = Math.ceil(miliseconds / 1000);
			
			var seconds:int = secondsTotal % 60;
			var minutes:int = int(secondsTotal / 60);
			
			var r:Object = new Object();
			r.seconds = seconds;
			r.minutes = minutes;
			
			return r;
		}
		
		import flash.utils.getDefinitionByName;
		public static function getMovie(name:String):MovieClip {
			var objClass:Class = getDefinitionByName(name) as Class;
			var mc:MovieClip = new objClass() as MovieClip;
			return mc;
		}
		
		public static function tint(clip:MovieClip, r:Number=1, g:Number=1, b:Number=1, a:Number=1):void {
			var matrix:Array   =   [	r, 0, 0, 0, 0,
										0, g, 0, 0, 0,
										0, 0, b, 0, 0,
										0, 0, 0, a, 0];
			var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);
			clip.filters = [cmf];
		}
		/*
		 * si queremos reducir 60 min a 40 min en el tiempo del reloj circular , entonces retrocederia hasta 
		 * el minuto 40
		 * use : example minutos : (40 min - 60 min , 60) = 40 min
		*/ 
		public static function strictMod(n:int , m:int):int {
			var r:int = r % m;
			if (r < 0) { 
				return r + m;
			}
			return r;
		}
		/*
		 * Funstion to Convertions Radians
		 * */
		public static function radToDegrees(angle:Number):Number {
			return ((angle * 180) / Math.PI);
		}
		
		public static function degreesToRad(angle:Number):Number {
			return ((angle * Math.PI) / 180);
		}
		
		/*
		 * Random min - max
		 * */
		public static function random(min:int , max:int):int {
			return min + int(Math.random() * (max - min + 1));
		}
		
		public static function randomDecimal(min:Number , max:Number):Number {
			return min + (Math.random()* (max - min));
		}
		/**
		 *  Permite mostrar con trace las propiedades de un objeto
		 * @param	obj
		 * @param	level
		 */
		public static function deepTrace( obj : *, level : int = 0 ) : void{
			var tabs : String = "";
			for ( var i : int = 0 ; i < level ; i++){ tabs += "\t" };
			
			for ( var prop : String in obj ){
				trace( tabs + "[" + prop + "] -> " + obj[ prop ] );
				deepTrace( obj[ prop ], level + 1 );
			}
		}
		
        public static function mixArrayRnd(array:Array , rnd:Rndm):Array {
            var mixed:Array = new Array();
            while (array.length > 0) {
                var r:int = rnd.integer(0 , array.length);
                mixed.push(array[r]);
                array.splice(r, 1);
            }
            return mixed;
        }
		
		public static function mixArray(array:Array):Array {
			var mixed:Array = new Array();
            while (array.length > 0) {
                var r:int = random(0 , array.length - 1);
                mixed.push(array[r]);
                array.splice(r, 1);
            }
            return mixed;
		}
        
        public static function cloneArray(array:Array):Array {
            return array.concat();
        }
		

		public static function gc():void {
			try {
				new LocalConnection().connect('foo');
				new LocalConnection().connect('foo');
			} catch (e:*) { }
		}
	}
}